<template>
    <div class="login">
        <h2>员工信息管理系统</h2>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="uname">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="upwd">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import qs from 'qs';
export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      return {
        ruleForm: {
          uername:'',
          pass: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3,  message: '长度至少 3 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.onLogin();
          } else {
            this.$message.error('登录失败');
            return false;
          }
        });
      },
      onLogin(){
        let info={username:this.ruleForm.username,password:this.ruleForm.pass};
        this.$http.post("https://7d5184241f35.ngrok.io/api/login",qs.stringify(info)).then((response)=>{
          console.log(response.data);
          if(response.data.flag===1){
            //成功了
            this.$router.push("/home");
            this.$message({message: '登陆成功',type: 'success'});
            this.$store.commit("updata",response.data.userinfo[0]);
            // console.log(this.$store.state.userinfo);
            
          }else{
            // 失败的
            this.$message.error('账号密码不正确');
            return false;
          }
        })
      }
    },
    
}
</script>
<style scoped>
    .login{
        width: 100%;
        height: 100%;
        background: url(../assets/img/login-bg.jpg) no-repeat;
        background-size: cover;
        position: absolute;
        top: 0;
        left: 0;
    }
    h2{
        width: 100%;
        text-align: center;
        line-height: 100px;
        font-size: 45px;
        margin-top: 100px;
        text-shadow: 5px 3px 5px #999;
    }
    .el-form{
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        background-color: rgba(233, 238, 243,.8);
        width: 400px;
        height: 200px;
        border-radius: 15px;
        text-align: center;
        padding: 40px 80px 0px 10px;
    }
    .el-button{
      margin-top: 15px;
    }
</style>